package com.xaicode.template.activemq.topic;

import com.activemq.demo.dataobject.IEImage;
import com.activemq.demo.dataobject.IEMsgHeader;
import com.activemq.demo.dataobject.IEVolume;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JMS_producer_topic_ichs {

    public static final String ACTIVEMQ_URL = "tcp://192.168.18.182:61616";
    public static final String TOPIC_NAME = "topic_2";


    /**
     * 1、一次运行订阅此公众号 注册一次 然后关闭程序
     * 2、再次运行依然可以收到持久化消息
     *
     * @param args
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
//        String msg = createReqMsg();
        String msg = createImageMsg();
//        String msg = createVolumnMsg();
        TextMessage textMessage = session.createTextMessage(msg);
        producer.send(textMessage);
        producer.close();
        session.close();
        connection.close();

        System.out.println("持久化TOPIC消息发送完成");
    }

    public static String createReqMsg() {
        IEMsgHeader ieMsgHeader = new IEMsgHeader();
        ieMsgHeader.setMsgType("CRZS_ICHS_REQ");
        Map<String, Object> map = new HashMap<>();
        map.put("IEMsgHeader", ieMsgHeader);
        Gson gson = new GsonBuilder().create();
        String s = gson.toJson(map);
        return s;
    }

    public static String createImageMsg() {
        IEMsgHeader ieMsgHeader = new IEMsgHeader();
        ieMsgHeader.setMsgType("CRZS_ICHS_IMAGE");
        Map<String, Object> map = new HashMap<>();
        map.put("IEMsgHeader", ieMsgHeader);

        IEImage ieImage = new IEImage();
        ieImage.setId("2");
        ieImage.setUrl("http://www.baidu.com/2344.jpg");
        ieImage.setCreateTime("2020-11-22 22:00:11");
        map.put("IEIchsImage", ieImage);


        Gson gson = new GsonBuilder().create();
        String s = gson.toJson(map);
        return s;
    }


    public static String createVolumnMsg() {
        IEMsgHeader ieMsgHeader = new IEMsgHeader();
        ieMsgHeader.setMsgType("CRZS_ICHS_VOLUME");
        Map<String, Object> map = new HashMap<>();
        map.put("IEMsgHeader", ieMsgHeader);

        IEVolume ieVolume = new IEVolume();
        ieVolume.setAreaId("3");
        ieVolume.setVolume("2333.88");
        ieVolume.setCreateTime("2020-11-23 00:00:00");

        IEVolume ieVolume1 = new IEVolume();
        ieVolume1.setAreaId("4");
        ieVolume1.setVolume("2334.88");
        ieVolume1.setCreateTime("2020-11-26 00:00:00");
        List<IEVolume> list = new ArrayList<>();
        list.add(ieVolume);
        list.add(ieVolume1);
        map.put("IEIchsVolume", list);
//        TypeUtils.compatibleWithJavaBean =true;
        Gson gson = new GsonBuilder().create();
        String s = gson.toJson(map);
//        String s = new JSONObject(map).toString();;
        return s;
    }
}
